package com.d4peng.test.jna;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;

import java.io.File;

/**
 * Jna:结构体类型
 *
 * @author d4peng
 * @date 2021-11-25
 */
public class JnaDemo3 {

    interface StructOps extends Library {
        public static class Person extends Structure {
            public String name;
            public int age;

            // 引用传递
            public static class ByReference extends Person implements Structure.ByReference {
            }

            // 值传递
            public static class ByValue extends Person implements Structure.ByValue {
            }
        }

        // 传入person的结构体,返回person的结构体
        Person.ByReference test(Person.ByReference byReference);
    }

    public static void main(String[] args) {
        String path = new File("").getAbsoluteFile().getPath() + "\\src\\main\\java\\com\\d4peng\\test\\jna\\c\\struct.dll";
        StructOps structOps = (StructOps) Native.loadLibrary(path, StructOps.class);

        StructOps.Person.ByReference byReference = new StructOps.Person.ByReference();
        byReference.age = 25;
        byReference.name = "d4peng";
        StructOps.Person.ByReference test = structOps.test(byReference);
        System.out.println(test.age);
        System.out.println(test.name);
    }
}
